'''功能层：封装业务流程'''
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

import sys

sys.path.append(r'..')

from web_base.base_page import BasePage


class DiscuzPage(BasePage):
    url = r'http://192.168.232.129/upload'
    # 登录定位
    username_loc = (By.ID, 'ls_username')
    password_loc = (By.ID, 'ls_password')
    login_loc = (By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button/em')
    # 退出定位
    logout_loc = (By.LINK_TEXT, '退出')

    # 注册定位(点击注册，姓名，密码，二次确认密码，邮箱，点击提交)
    register_loc = (By.LINK_TEXT, '立即注册')
    register_name_loc = (By.ID, 'p0dS30')
    register_password_loc = (By.ID, 'jjQlGz')
    register_pwd_loc = (By.ID, 'apeqQ6')
    register_email_loc = (By.ID, 'jp2PfX')
    register_submit_loc = (By.ID, 'registerformsubmit')

    # 看帖定位(read_m_loc点击模块，read_t_loc点击帖子)
    read_m_loc = (By.XPATH, '//*[@id="category_1"]/table/tbody/tr[2]/td[2]/h2/a')
    read_t_loc = (By.XPATH, '//*[@id="normalthread_612"]/tr/th/a[2]')

    # 发帖定位(点击模块，点击发帖，输入标题，输入内容，点击提交)
    post_m_loc = (By.XPATH, '//*[@id="category_1"]/table/tbody/tr[2]/td[2]/h2/a')

    post_new_loc = (By.ID, 'newspecial')
    post_subject_loc = (By.ID, 'subject')
    post_message_loc = (By.XPATH, '/html/body')
    post_submit_loc = (By.ID, 'postsubmit')

    # 回帖定位
    reply_m_loc = (By.XPATH, '//*[@id="category_1"]/table/tbody/tr[2]/td[2]/h2/a')
    reply_t_loc = (By.XPATH, '//*[@id="normalthread_612"]/tr/th/a[2]')
    reply_message_loc = (By.ID, 'fastpostmessage')
    reply_submit_loc = (By.ID, 'fastpostsubmit')

    def __init__(self, driver):
        super().__init__(driver)
        self.get_url(self.url)

    def login(self, username, password):
        # 登录
        self.ele_clear(self.username_loc)
        self.input_ele(self.username_loc, username)
        self.ele_clear(self.password_loc)
        self.input_ele(self.password_loc, password)
        self.ele_click(self.login_loc)

    def logout(self):
        self.ele_click(self.logout_loc)
        sleep(2)

    def register(self, name, password, pwd, email):
        self.ele_click(self.register_loc)
        self.ele_clear(self.register_name_loc)
        self.input_ele(self.register_name_loc, name)
        self.ele_clear(self.register_password_loc)
        self.input_ele(self.register_password_loc, password)
        self.ele_clear(self.register_pwd_loc)
        self.input_ele(self.register_pwd_loc, pwd)
        self.ele_clear(self.register_email_loc)
        self.input_ele(self.register_email_loc, email)
        self.ele_click(self.register_submit_loc)
        sleep(3)

    def read(self, username='admin', password='123456'):
        self.login(username, password)
        self.ele_click(self.read_m_loc)
        self.ele_click(self.read_t_loc)

    def post(self, subject, message, username='admin', password='123456'):
        self.login(username, password)
        sleep(2)

        self.ele_click(self.post_m_loc)
        self.ele_click(self.post_new_loc)
        self.ele_clear(self.post_subject_loc)
        self.input_ele(self.post_subject_loc, subject)
        self.driver.switch_to.frame('e_iframe')
        self.ele_clear(self.post_message_loc)
        self.input_ele(self.post_message_loc, message)
        self.driver.switch_to.default_content()
        self.ele_click(self.post_submit_loc)

    def reply(self, connect):
        self.ele_click(self.reply_m_loc)
        self.ele_click(self.reply_t_loc)
        self.ele_clear(self.reply_message_loc)
        self.input_ele(self.reply_message_loc, connect)
        self.ele_click(self.reply_submit_loc)


if __name__ == '__main__':
    # 测试代码，没有引入option
    chrome_location = r'E:\python_pro\Win_x64_1135105_chrome-win\chrome-win\chrome.exe'
    options = webdriver.ChromeOptions()
    options.binary_location = chrome_location
    driver = webdriver.Chrome(options=options)

    discuz_page = DiscuzPage(driver)
    # username = 'admin'
    # password = '123456'
    # discuz_page.assert(username, password)

    # # 测试注册
    # name = 'ppp123'
    # password = '123456'
    # pwd = '123456'
    # email = '1234595@qq.com'
    # discuz_page.register(name, password, pwd, email)

    # 测试看帖
    # discuz_page.read()

    # 测试发帖
    # subject = '11111'
    # message = '11111111'
    # discuz_page.post(subject, message)
    # driver.quit()

    # 测试回帖
    connect = '999999'
    discuz_page.reply(connect)